<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Log Gamma</title>
<link rel="stylesheet" href="../../math.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Math Toolkit 4.2.1">
<link rel="up" href="../sf_gamma.html" title="Gamma Functions">
<link rel="prev" href="tgamma.html" title="Gamma">
<link rel="next" href="digamma.html" title="Digamma">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="tgamma.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../sf_gamma.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="digamma.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="math_toolkit.sf_gamma.lgamma"></a><a class="link" href="lgamma.html" title="Log Gamma">Log Gamma</a>
</h3></div></div></div>
<h5>
<a name="math_toolkit.sf_gamma.lgamma.h0"></a>
        <span class="phrase"><a name="math_toolkit.sf_gamma.lgamma.synopsis"></a></span><a class="link" href="lgamma.html#math_toolkit.sf_gamma.lgamma.synopsis">Synopsis</a>
      </h5>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">special_functions</span><span class="special">/</span><span class="identifier">gamma</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
</pre>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">math</span><span class="special">{</span>

<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
<a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">lgamma</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">z</span><span class="special">);</span>

<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <a class="link" href="../../policy.html" title="Chapter 22. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&gt;</span>
<a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">lgamma</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">z</span><span class="special">,</span> <span class="keyword">const</span> <a class="link" href="../../policy.html" title="Chapter 22. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&amp;);</span>

<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
<a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">lgamma</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">z</span><span class="special">,</span> <span class="keyword">int</span><span class="special">*</span> <span class="identifier">sign</span><span class="special">);</span>

<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <a class="link" href="../../policy.html" title="Chapter 22. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&gt;</span>
<a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">lgamma</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">z</span><span class="special">,</span> <span class="keyword">int</span><span class="special">*</span> <span class="identifier">sign</span><span class="special">,</span> <span class="keyword">const</span> <a class="link" href="../../policy.html" title="Chapter 22. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&amp;);</span>

<span class="special">}}</span> <span class="comment">// namespaces</span>
</pre>
<h5>
<a name="math_toolkit.sf_gamma.lgamma.h1"></a>
        <span class="phrase"><a name="math_toolkit.sf_gamma.lgamma.description"></a></span><a class="link" href="lgamma.html#math_toolkit.sf_gamma.lgamma.description">Description</a>
      </h5>
<p>
        The <a href="http://en.wikipedia.org/wiki/Gamma_function" target="_top">lgamma function</a>
        is defined by:
      </p>
<div class="blockquote"><blockquote class="blockquote"><p>
          <span class="inlinemediaobject"><img src="../../../equations/lgamm1.svg"></span>

        </p></blockquote></div>
<p>
        The second form of the function takes a pointer to an integer, which if non-null
        is set on output to the sign of tgamma(z).
      </p>
<p>
        The final <a class="link" href="../../policy.html" title="Chapter 22. Policies: Controlling Precision, Error Handling etc">Policy</a> argument is optional and can
        be used to control the behaviour of the function: how it handles errors,
        what level of precision to use etc. Refer to the <a class="link" href="../../policy.html" title="Chapter 22. Policies: Controlling Precision, Error Handling etc">policy
        documentation for more details</a>.
      </p>
<div class="blockquote"><blockquote class="blockquote"><p>
          <span class="inlinemediaobject"><img src="../../../graphs/lgamma.svg" align="middle"></span>

        </p></blockquote></div>
<p>
        The return type of these functions is computed using the <a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>result
        type calculation rules</em></span></a>: the result is of type <code class="computeroutput"><span class="keyword">double</span></code> if T is an integer type, or type T
        otherwise.
      </p>
<h5>
<a name="math_toolkit.sf_gamma.lgamma.h2"></a>
        <span class="phrase"><a name="math_toolkit.sf_gamma.lgamma.accuracy"></a></span><a class="link" href="lgamma.html#math_toolkit.sf_gamma.lgamma.accuracy">Accuracy</a>
      </h5>
<p>
        The following table shows the peak errors (in units of epsilon) found on
        various platforms with various floating point types, along with comparisons
        to various other libraries. Unless otherwise specified any floating point
        type that is narrower than the one shown will have <a class="link" href="../relative_error.html#math_toolkit.relative_error.zero_error">effectively
        zero error</a>.
      </p>
<p>
        Note that while the relative errors near the positive roots of lgamma are
        very low, the lgamma function has an infinite number of irrational roots
        for negative arguments: very close to these negative roots only a low absolute
        error can be guaranteed.
      </p>
<div class="table">
<a name="math_toolkit.sf_gamma.lgamma.table_lgamma"></a><p class="title"><b>Table 8.3. Error rates for lgamma</b></p>
<div class="table-contents"><table class="table" summary="Error rates for lgamma">
<colgroup>
<col>
<col>
<col>
<col>
<col>
</colgroup>
<thead><tr>
<th>
              </th>
<th>
                <p>
                  GNU C++ version 7.1.0<br> linux<br> double
                </p>
              </th>
<th>
                <p>
                  GNU C++ version 7.1.0<br> linux<br> long double
                </p>
              </th>
<th>
                <p>
                  Sun compiler version 0x5150<br> Sun Solaris<br> long double
                </p>
              </th>
<th>
                <p>
                  Microsoft Visual C++ version 14.1<br> Win32<br> double
                </p>
              </th>
</tr></thead>
<tbody>
<tr>
<td>
                <p>
                  factorials
                </p>
              </td>
<td>
                <p>
                  <span class="blue">Max = 0ε (Mean = 0ε)</span><br> <br> (<span class="emphasis"><em>GSL
                  2.1:</em></span> Max = 33.6ε (Mean = 2.78ε))<br> (<span class="emphasis"><em>Rmath
                  3.2.3:</em></span> Max = 1.55ε (Mean = 0.592ε))
                </p>
              </td>
<td>
                <p>
                  <span class="blue">Max = 0.991ε (Mean = 0.308ε)</span><br> <br>
                  (<span class="emphasis"><em>&lt;cmath&gt;:</em></span> Max = 1.67ε (Mean = 0.487ε))<br>
                  (<span class="emphasis"><em>&lt;math.h&gt;:</em></span> Max = 1.67ε (Mean = 0.487ε))
                </p>
              </td>
<td>
                <p>
                  <span class="blue">Max = 0.991ε (Mean = 0.383ε)</span><br> <br>
                  (<span class="emphasis"><em>&lt;math.h&gt;:</em></span> Max = 1.36ε (Mean = 0.476ε))
                </p>
              </td>
<td>
                <p>
                  <span class="blue">Max = 0.914ε (Mean = 0.175ε)</span><br> <br>
                  (<span class="emphasis"><em>&lt;math.h&gt;:</em></span> Max = 0.958ε (Mean = 0.38ε))
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  near 0
                </p>
              </td>
<td>
                <p>
                  <span class="blue">Max = 0ε (Mean = 0ε)</span><br> <br> (<span class="emphasis"><em>GSL
                  2.1:</em></span> Max = 5.21ε (Mean = 1.57ε))<br> (<span class="emphasis"><em>Rmath
                  3.2.3:</em></span> Max = 0ε (Mean = 0ε))
                </p>
              </td>
<td>
                <p>
                  <span class="blue">Max = 1.42ε (Mean = 0.566ε)</span><br> <br>
                  (<span class="emphasis"><em>&lt;cmath&gt;:</em></span> Max = 0.964ε (Mean = 0.543ε))<br>
                  (<span class="emphasis"><em>&lt;math.h&gt;:</em></span> Max = 0.964ε (Mean = 0.543ε))
                </p>
              </td>
<td>
                <p>
                  <span class="blue">Max = 1.42ε (Mean = 0.566ε)</span><br> <br>
                  (<span class="emphasis"><em>&lt;math.h&gt;:</em></span> Max = 0.964ε (Mean = 0.543ε))
                </p>
              </td>
<td>
                <p>
                  <span class="blue">Max = 0.964ε (Mean = 0.462ε)</span><br> <br>
                  (<span class="emphasis"><em>&lt;math.h&gt;:</em></span> Max = 0.962ε (Mean = 0.372ε))
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  near 1
                </p>
              </td>
<td>
                <p>
                  <span class="blue">Max = 0ε (Mean = 0ε)</span><br> <br> (<span class="emphasis"><em>GSL
                  2.1:</em></span> Max = 442ε (Mean = 88.8ε))<br> (<span class="emphasis"><em>Rmath
                  3.2.3:</em></span> Max = 7.99e+04ε (Mean = 1.68e+04ε))
                </p>
              </td>
<td>
                <p>
                  <span class="blue">Max = 0.948ε (Mean = 0.36ε)</span><br> <br>
                  (<span class="emphasis"><em>&lt;cmath&gt;:</em></span> Max = 0.615ε (Mean = 0.096ε))<br>
                  (<span class="emphasis"><em>&lt;math.h&gt;:</em></span> Max = 0.615ε (Mean = 0.096ε))
                </p>
              </td>
<td>
                <p>
                  <span class="blue">Max = 0.948ε (Mean = 0.36ε)</span><br> <br>
                  (<span class="emphasis"><em>&lt;math.h&gt;:</em></span> Max = 1.71ε (Mean = 0.581ε))
                </p>
              </td>
<td>
                <p>
                  <span class="blue">Max = 0.867ε (Mean = 0.468ε)</span><br> <br>
                  (<span class="emphasis"><em>&lt;math.h&gt;:</em></span> Max = 0.906ε (Mean = 0.565ε))
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  near 2
                </p>
              </td>
<td>
                <p>
                  <span class="blue">Max = 0ε (Mean = 0ε)</span><br> <br> (<span class="emphasis"><em>GSL
                  2.1:</em></span> Max = 1.17e+03ε (Mean = 274ε))<br> (<span class="emphasis"><em>Rmath
                  3.2.3:</em></span> Max = 2.63e+05ε (Mean = 5.84e+04ε))
                </p>
              </td>
<td>
                <p>
                  <span class="blue">Max = 0.878ε (Mean = 0.242ε)</span><br> <br>
                  (<span class="emphasis"><em>&lt;cmath&gt;:</em></span> Max = 0.741ε (Mean = 0.263ε))<br>
                  (<span class="emphasis"><em>&lt;math.h&gt;:</em></span> Max = 0.741ε (Mean = 0.263ε))
                </p>
              </td>
<td>
                <p>
                  <span class="blue">Max = 0.878ε (Mean = 0.242ε)</span><br> <br>
                  (<span class="emphasis"><em>&lt;math.h&gt;:</em></span> Max = 0.598ε (Mean = 0.235ε))
                </p>
              </td>
<td>
                <p>
                  <span class="blue">Max = 0.591ε (Mean = 0.159ε)</span><br> <br>
                  (<span class="emphasis"><em>&lt;math.h&gt;:</em></span> Max = 0.741ε (Mean = 0.473ε))
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  near -10
                </p>
              </td>
<td>
                <p>
                  <span class="blue">Max = 0ε (Mean = 0ε)</span><br> <br> (<span class="emphasis"><em>GSL
                  2.1:</em></span> Max = 24.9ε (Mean = 4.6ε))<br> (<span class="emphasis"><em>Rmath
                  3.2.3:</em></span> Max = 4.22ε (Mean = 1.26ε))
                </p>
              </td>
<td>
                <p>
                  <span class="blue">Max = 3.81ε (Mean = 1.01ε)</span><br> <br>
                  (<span class="emphasis"><em>&lt;cmath&gt;:</em></span> Max = 0.997ε (Mean = 0.412ε))<br>
                  (<span class="emphasis"><em>&lt;math.h&gt;:</em></span> Max = 0.997ε (Mean = 0.412ε))
                </p>
              </td>
<td>
                <p>
                  <span class="blue">Max = 3.81ε (Mean = 1.01ε)</span><br> <br>
                  (<span class="emphasis"><em>&lt;math.h&gt;:</em></span> Max = 3.04ε (Mean = 1.01ε))
                </p>
              </td>
<td>
                <p>
                  <span class="blue">Max = 4.22ε (Mean = 1.33ε)</span><br> <br>
                  (<span class="emphasis"><em>&lt;math.h&gt;:</em></span> Max = 0.997ε (Mean = 0.444ε))
                </p>
              </td>
</tr>
<tr>
<td>
                <p>
                  near -55
                </p>
              </td>
<td>
                <p>
                  <span class="blue">Max = 0ε (Mean = 0ε)</span><br> <br> (<span class="emphasis"><em>GSL
                  2.1:</em></span> Max = 7.02ε (Mean = 1.47ε))<br> (<span class="emphasis"><em>Rmath
                  3.2.3:</em></span> Max = 250ε (Mean = 60.9ε))
                </p>
              </td>
<td>
                <p>
                  <span class="blue">Max = 0.821ε (Mean = 0.513ε)</span><br> <br>
                  (<span class="emphasis"><em>&lt;cmath&gt;:</em></span> Max = 1.58ε (Mean = 0.672ε))<br>
                  (<span class="emphasis"><em>&lt;math.h&gt;:</em></span> Max = 1.58ε (Mean = 0.672ε))
                </p>
              </td>
<td>
                <p>
                  <span class="blue">Max = 1.59ε (Mean = 0.587ε)</span><br> <br>
                  (<span class="emphasis"><em>&lt;math.h&gt;:</em></span> Max = 0.821ε (Mean = 0.674ε))
                </p>
              </td>
<td>
                <p>
                  <span class="blue">Max = 0.821ε (Mean = 0.419ε)</span><br> <br>
                  (<span class="emphasis"><em>&lt;math.h&gt;:</em></span> Max = 249ε (Mean = 43.1ε))
                </p>
              </td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break"><p>
        The following error plot are based on an exhaustive search of the functions
        domain, MSVC-15.5 at <code class="computeroutput"><span class="keyword">double</span></code>
        precision, and GCC-7.1/Ubuntu for <code class="computeroutput"><span class="keyword">long</span>
        <span class="keyword">double</span></code> and <code class="computeroutput"><span class="identifier">__float128</span></code>.
      </p>
<div class="blockquote"><blockquote class="blockquote"><p>
          <span class="inlinemediaobject"><img src="../../../graphs/lgamma__double.svg" align="middle"></span>

        </p></blockquote></div>
<div class="blockquote"><blockquote class="blockquote"><p>
          <span class="inlinemediaobject"><img src="../../../graphs/lgamma__80_bit_long_double.svg" align="middle"></span>

        </p></blockquote></div>
<div class="blockquote"><blockquote class="blockquote"><p>
          <span class="inlinemediaobject"><img src="../../../graphs/lgamma____float128.svg" align="middle"></span>

        </p></blockquote></div>
<h5>
<a name="math_toolkit.sf_gamma.lgamma.h3"></a>
        <span class="phrase"><a name="math_toolkit.sf_gamma.lgamma.testing"></a></span><a class="link" href="lgamma.html#math_toolkit.sf_gamma.lgamma.testing">Testing</a>
      </h5>
<p>
        The main tests for this function involve comparisons against the logs of
        the factorials which can be independently calculated to very high accuracy.
      </p>
<p>
        Random tests in key problem areas are also used.
      </p>
<h5>
<a name="math_toolkit.sf_gamma.lgamma.h4"></a>
        <span class="phrase"><a name="math_toolkit.sf_gamma.lgamma.implementation"></a></span><a class="link" href="lgamma.html#math_toolkit.sf_gamma.lgamma.implementation">Implementation</a>
      </h5>
<p>
        The generic version of this function is implemented using Sterling's approximation
        for large arguments:
      </p>
<div class="blockquote"><blockquote class="blockquote"><p>
          <span class="inlinemediaobject"><img src="../../../equations/gamma6.svg"></span>

        </p></blockquote></div>
<p>
        For small arguments, the logarithm of tgamma is used.
      </p>
<p>
        For negative <span class="emphasis"><em>z</em></span> the logarithm version of the reflection
        formula is used:
      </p>
<div class="blockquote"><blockquote class="blockquote"><p>
          <span class="inlinemediaobject"><img src="../../../equations/lgamm3.svg"></span>

        </p></blockquote></div>
<p>
        For types of known precision, the <a class="link" href="../lanczos.html" title="The Lanczos Approximation">Lanczos
        approximation</a> is used, a traits class <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">lanczos</span><span class="special">::</span><span class="identifier">lanczos_traits</span></code>
        maps type T to an appropriate approximation. The logarithmic version of the
        <a class="link" href="../lanczos.html" title="The Lanczos Approximation">Lanczos approximation</a> is:
      </p>
<div class="blockquote"><blockquote class="blockquote"><p>
          <span class="inlinemediaobject"><img src="../../../equations/lgamm4.svg"></span>

        </p></blockquote></div>
<p>
        Where L<sub>e,g</sub> is the Lanczos sum, scaled by e<sup>g</sup>.
      </p>
<p>
        As before the reflection formula is used for <span class="emphasis"><em>z &lt; 0</em></span>.
      </p>
<p>
        When z is very near 1 or 2, then the logarithmic version of the <a class="link" href="../lanczos.html" title="The Lanczos Approximation">Lanczos
        approximation</a> suffers very badly from cancellation error: indeed for
        values sufficiently close to 1 or 2, arbitrarily large relative errors can
        be obtained (even though the absolute error is tiny).
      </p>
<p>
        For types with up to 113 bits of precision (up to and including 128-bit long
        doubles), root-preserving rational approximations <a class="link" href="../sf_implementation.html#math_toolkit.sf_implementation.rational_approximations_used">devised
        by JM</a> are used over the intervals [1,2] and [2,3]. Over the interval
        [2,3] the approximation form used is:
      </p>
<pre class="programlisting"><span class="identifier">lgamma</span><span class="special">(</span><span class="identifier">z</span><span class="special">)</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">z</span><span class="special">-</span><span class="number">2</span><span class="special">)(</span><span class="identifier">z</span><span class="special">+</span><span class="number">1</span><span class="special">)(</span><span class="identifier">Y</span> <span class="special">+</span> <span class="identifier">R</span><span class="special">(</span><span class="identifier">z</span><span class="special">-</span><span class="number">2</span><span class="special">));</span>
</pre>
<p>
        Where Y is a constant, and R(z-2) is the rational approximation: optimised
        so that its absolute error is tiny compared to Y. In addition, small values
        of z greater than 3 can handled by argument reduction using the recurrence
        relation:
      </p>
<pre class="programlisting"><span class="identifier">lgamma</span><span class="special">(</span><span class="identifier">z</span><span class="special">+</span><span class="number">1</span><span class="special">)</span> <span class="special">=</span> <span class="identifier">log</span><span class="special">(</span><span class="identifier">z</span><span class="special">)</span> <span class="special">+</span> <span class="identifier">lgamma</span><span class="special">(</span><span class="identifier">z</span><span class="special">);</span>
</pre>
<p>
        Over the interval [1,2] two approximations have to be used, one for small
        z uses:
      </p>
<pre class="programlisting"><span class="identifier">lgamma</span><span class="special">(</span><span class="identifier">z</span><span class="special">)</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">z</span><span class="special">-</span><span class="number">1</span><span class="special">)(</span><span class="identifier">z</span><span class="special">-</span><span class="number">2</span><span class="special">)(</span><span class="identifier">Y</span> <span class="special">+</span> <span class="identifier">R</span><span class="special">(</span><span class="identifier">z</span><span class="special">-</span><span class="number">1</span><span class="special">));</span>
</pre>
<p>
        Once again Y is a constant, and R(z-1) is optimised for low absolute error
        compared to Y. For z &gt; 1.5 the above form wouldn't converge to a minimax
        solution but this similar form does:
      </p>
<pre class="programlisting"><span class="identifier">lgamma</span><span class="special">(</span><span class="identifier">z</span><span class="special">)</span> <span class="special">=</span> <span class="special">(</span><span class="number">2</span><span class="special">-</span><span class="identifier">z</span><span class="special">)(</span><span class="number">1</span><span class="special">-</span><span class="identifier">z</span><span class="special">)(</span><span class="identifier">Y</span> <span class="special">+</span> <span class="identifier">R</span><span class="special">(</span><span class="number">2</span><span class="special">-</span><span class="identifier">z</span><span class="special">));</span>
</pre>
<p>
        Finally for z &lt; 1 the recurrence relation can be used to move to z &gt;
        1:
      </p>
<pre class="programlisting"><span class="identifier">lgamma</span><span class="special">(</span><span class="identifier">z</span><span class="special">)</span> <span class="special">=</span> <span class="identifier">lgamma</span><span class="special">(</span><span class="identifier">z</span><span class="special">+</span><span class="number">1</span><span class="special">)</span> <span class="special">-</span> <span class="identifier">log</span><span class="special">(</span><span class="identifier">z</span><span class="special">);</span>
</pre>
<p>
        Note that while this involves a subtraction, it appears not to suffer from
        cancellation error: as z decreases from 1 the <code class="computeroutput"><span class="special">-</span><span class="identifier">log</span><span class="special">(</span><span class="identifier">z</span><span class="special">)</span></code> term grows positive much more rapidly than
        the <code class="computeroutput"><span class="identifier">lgamma</span><span class="special">(</span><span class="identifier">z</span><span class="special">+</span><span class="number">1</span><span class="special">)</span></code> term becomes negative. So in this specific
        case, significant digits are preserved, rather than cancelled.
      </p>
<p>
        For other types which do have a <a class="link" href="../lanczos.html" title="The Lanczos Approximation">Lanczos
        approximation</a> defined for them the current solution is as follows:
        imagine we balance the two terms in the <a class="link" href="../lanczos.html" title="The Lanczos Approximation">Lanczos
        approximation</a> by dividing the power term by its value at <span class="emphasis"><em>z
        = 1</em></span>, and then multiplying the Lanczos coefficients by the same
        value. Now each term will take the value 1 at <span class="emphasis"><em>z = 1</em></span>
        and we can rearrange the power terms in terms of log1p. Likewise if we subtract
        1 from the Lanczos sum part (algebraically, by subtracting the value of each
        term at <span class="emphasis"><em>z = 1</em></span>), we obtain a new summation that can be
        also be fed into log1p. Crucially, all of the terms tend to zero, as <span class="emphasis"><em>z
        -&gt; 1</em></span>:
      </p>
<div class="blockquote"><blockquote class="blockquote"><p>
          <span class="inlinemediaobject"><img src="../../../equations/lgamm5.svg"></span>

        </p></blockquote></div>
<p>
        The C<sub>k</sub> terms in the above are the same as in the <a class="link" href="../lanczos.html" title="The Lanczos Approximation">Lanczos
        approximation</a>.
      </p>
<p>
        A similar rearrangement can be performed at <span class="emphasis"><em>z = 2</em></span>:
      </p>
<div class="blockquote"><blockquote class="blockquote"><p>
          <span class="inlinemediaobject"><img src="../../../equations/lgamm6.svg"></span>

        </p></blockquote></div>
</div>
<div class="copyright-footer">Copyright © 2006-2021 Nikhar Agrawal, Anton Bikineev, Matthew Borland,
      Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert Holin, Bruno
      Lalande, John Maddock, Evan Miller, Jeremy Murphy, Matthew Pulver, Johan Råde,
      Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg, Daryle
      Walker and Xiaogang Zhang<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="tgamma.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../sf_gamma.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="digamma.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
